package excel_writer

import (
	"encoding/csv"
	"golang.org/x/text/encoding/simplifiedchinese"
	"golang.org/x/text/transform"
	"log"
	"os"
	"path/filepath"
)

func CreateExcelFile(path string, fileName string) (*os.File, *csv.Writer) {

	// 确保目录存在
	err := os.MkdirAll(path, 0755)
	if err != nil {
		log.Fatalf("无法创建目录: %v", err)
	}

	// 创建CSV文件
	filePath := filepath.Join(path, fileName)
	file, err := os.Create(filePath)
	if err != nil {
		log.Fatalf("无法创建文件: %v", err)
	}

	// 创建CSV写入器 将写入器转换为GBK编码
	writer := csv.NewWriter(transform.NewWriter(file, simplifiedchinese.GBK.NewEncoder()))

	// 写入标题行
	header := []string{"课程编号", "课程名", "课程URL", "学校"}
	if err := writer.Write(header); err != nil {
		log.Fatalf("无法写入标题行: %v", err)
	}

	return file, writer
}
